from pwn import *

from time import sleep
import sys
import cipher

r = remote(sys.argv[1], "65503")
KEY = "sadadasffsdfasgasdgafsadfsadfasf"

ciph = cipher.Cipher(KEY)

try:
    while True:
        data = b""
        command = input("command: ").strip()
        if command in ['door']:
            r.send(command)
        else:
            print("ENC")
            print("msg =", command)
            d = ciph.Encrypt(command)
            print("encmsg= ", d)
            r.send(d)
        if command in ['door']:
            d = r.recv()
            print("d = ", d)
            #print("d (decrypted) = ", ciph.Decrypt(d))
            continue
        #data = ciph.Decrypt(r.recv())
        #data = r.Recieve() #ciph.Decrypt(r.recvall())
        while True:
            temp = r.recv()
            if temp:
                data += temp
            if len(temp)<1024:
                break
        data = ciph.Decrypt(data)
        #data = r.recvall()
        #with open("output.bin", '+w') as fout:
        #    fout.write(data)
        with open("flags.txt", "w") as f:
            f.write(data)
        #print(ciph.Decrypt(data))
except Exception as ex:
    print(ex)
    r.close()
